import org.jnativehook.DefaultDispatchService;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
import org.jnativehook.mouse.NativeMouseEvent;
import org.jnativehook.mouse.NativeMouseInputListener;
import org.jnativehook.mouse.NativeMouseWheelEvent;
import org.jnativehook.mouse.NativeMouseWheelListener;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ScanDemo implements NativeKeyListener, NativeMouseInputListener, NativeMouseWheelListener {

    public static void main(String[] args){
        new ScanDemo();
    }

    public ScanDemo(){
        GlobalScreen.setEventDispatcher(new DefaultDispatchService());
        try {
            GlobalScreen.registerNativeHook();

        } catch (NativeHookException e) {
            e.printStackTrace();
        }
        GlobalScreen.addNativeKeyListener(this);
        GlobalScreen.addNativeMouseListener(this);
        GlobalScreen.addNativeMouseMotionListener(this);
    }


    private long lastTime = 0;
    private String lastCode = null;
    /**
     * @see NativeKeyListener#nativeKeyPressed(NativeKeyEvent)
     */
    @Override
    public void nativeKeyPressed(NativeKeyEvent e) {
        int keyCode = e.getKeyCode();
        int enterCode = NativeKeyEvent.VC_ENTER;

        int scanTimeSpan = 50;
        int keyboardTimeSpan = 100;

        long currTime = System.currentTimeMillis();
        String currCode = NativeKeyEvent.getKeyText(keyCode);

        if (lastTime > 0) {
            if (currTime - lastTime <= scanTimeSpan && keyCode != enterCode) {
                lastCode += currCode;
            } else if (currTime - lastTime > keyboardTimeSpan) {
                lastCode = null;
            }
        } else {
            lastCode = currCode;
        }

        lastTime = currTime;

        if (keyCode == enterCode) {

            if(lastCode != null) {
                lastCode = lastCode.trim();
            }

            System.out.println("keyCode:"+e.getKeyCode()+" keyText:"+currCode+" scanCode:("+lastCode+")");

            lastTime = 0;
            lastCode = null;
        }

    }

    /**
     * @see NativeKeyListener#nativeKeyReleased(NativeKeyEvent)
     */
    @Override
    public void nativeKeyReleased(NativeKeyEvent e) {

        //System.out.println(e.paramString());
    }

    /**k
     * @see NativeKeyListener#nativeKeyTyped(NativeKeyEvent)
     */
    @Override
    public void nativeKeyTyped(NativeKeyEvent e) {

        //System.out.println(e.paramString());
    }

    /**
     * @see (NativeMouseEvent)
     */
    @Override
    public void nativeMouseMoved(NativeMouseEvent e) {
        //System.out.println(e.paramString());
    }

    @Override
    public void nativeMouseDragged(NativeMouseEvent nativeMouseEvent) {

    }

    @Override
    public void nativeMouseClicked(NativeMouseEvent nativeMouseEvent) {

    }

    @Override
    public void nativeMousePressed(NativeMouseEvent nativeMouseEvent) {

    }

    @Override
    public void nativeMouseReleased(NativeMouseEvent nativeMouseEvent) {

    }

    @Override
    public void nativeMouseWheelMoved(NativeMouseWheelEvent nativeMouseWheelEvent) {

    }
}

